package com.mxpio.erp.plan.controller;

import com.mxpio.erp.common.plan.entity.MpsLineTemp;
import com.mxpio.erp.common.plan.service.MpsLineTempService;
import com.mxpioframework.common.vo.Result;
import com.mxpioframework.jpa.query.Criteria;
import com.mxpioframework.jpa.query.Operator;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import java.io.UnsupportedEncodingException;
import java.util.List;

@Tag(name = "MpsLineTempController", description = "主计划清单temp接口")
@RestController
@RequestMapping("/erp/plan/mpstemp/linetemp/")
public class MpsLineTempController {

	@Autowired
	private MpsLineTempService mpsLineTempService;

	@GetMapping("list/{pid}")
	@Operation(summary = "主计划清单temp列表", description = "获取主计划清单temp列表（无分页）", method = "GET")
	public Result<List<MpsLineTemp>> list(@Parameter(description = "查询参数") Criteria criteria,
			@PathVariable(name = "pid") String pid)
			throws UnsupportedEncodingException {
		criteria.addCriterion("pid", Operator.EQ, pid);
		List<MpsLineTemp> mpsLine = mpsLineTempService.list(MpsLineTemp.class, criteria);
		mpsLineTempService.associatedTransientFields(mpsLine);
		return Result.OK(mpsLine);
	}
	
	@GetMapping("page/{pid}")
	@Operation(summary = "主计划清单temp列表", description = "获取主计划清单temp列表（分页）", method = "GET")
	public Result<Page<MpsLineTemp>> page(@Parameter(description = "查询参数") Criteria criteria,
			@PathVariable(name = "pid") String pid,
			@Parameter(description = "每页条数") Integer pageSize, @Parameter(description = "页号") Integer pageNo)
			throws UnsupportedEncodingException {
		Pageable page = PageRequest.of(pageNo - 1, pageSize);
		criteria.addCriterion("pid", Operator.EQ, pid);
		Page<MpsLineTemp> mpsLine = mpsLineTempService.listPage(MpsLineTemp.class, page, criteria);
		mpsLineTempService.associatedTransientFields(mpsLine.getContent());
		return Result.OK(mpsLine);
	}
	
	
/*
	@GetMapping("list/{id}")
	@Operation(summary = "根据id获取主计划清单temp", description = "根据id获取主计划清单temp", method = "GET")
	public Result<MpsLineTemp> getById(
			@Parameter(description = "ID") @PathVariable(name = "id", required = true) String id) {
		MpsLineTemp mpsLine = mpsLineTempService.getById(MpsLineTemp.class, id);
		return Result.OK(mpsLine);
	}

	@RequestMapping("save")
	@Operation(summary = "保存主计划清单temp", description = "保存主计划清单temp")
	public Result<List<MpsLineTemp>> save(@RequestBody List<MpsLineTemp> mpsLines) {
		mpsLineTempService.save(mpsLines);
		return Result.OK(mpsLines);
	}

	@DeleteMapping("remove/{ids}")
	@Operation(summary = "根据ids删除主计划清单temp", description = "删除主计划清单temp", method = "DELETE")
	@Transactional(readOnly = false)
	public Result<MpsLineTemp> remove(
			@Parameter(description = "id，多个单号用英文逗号分隔") @PathVariable(name = "ids", required = true) String ids) {
		String[] id = ids.split(",");
		for (String key : id) {
			mpsLineTempService.delete(MpsLineTemp.class, key);
		}
		return Result.OK();
	}*/
}
